﻿
 
 function CustomPropertyElement (data)
 {
	 this.id=data['id'] || '0';
	 this.property_name=data['property_name'] || 'Untitled Custom Property';
	 this.property_type=data['property_type'] || '1';
	 this.property_each=data['property_each'] || '';
	 this.display_text=data['display_text'] || '';
	 this.type_name=data['type_name'];
	 this.valuelist=[]; 
	 this.draw = function (value,off)
	 { 
		 if (!value) value='';
		 var dis=off=='disabled'?'disabled':''
		 var half=ElementResizeListener.state==2?'<br>':'</td><td>'
		 var htm=this.property_name + ': <input value="' + this.type_name + '">' + this.valuelist.length;
		 htm='<tr><td class=small>' + this.property_name + half;
		 var multi_script = ' onchange="MultiAddorRemoveProperty(\''+this.property_name+'\', this)" ';
		 var select_script = ' onchange="AddorRemoveProperty(\''+this.property_name+'\', this.value, this.selectedIndex>0)" ';
		 var input_script = ' onchange="AddorRemoveProperty(\''+this.property_name+'\', this.value, this.value.length>0)" ';
		 var checkbox_script = ' onchange="AddorRemoveProperty(\''+this.property_name+'\', 1, this.checked)" ';
		 switch (this.type_name)
		 {
			 case 'dropdown':
			 	htm += '<select '+dis+'' + select_script + ' style="font-size:9pt">';
			 	htm += '<option>(choose)';
				for (var x in this.valuelist)
				{
					htm += '<option value="' + this.valuelist[x] + '" ' + (value==this.valuelist[x]?' selected ':'') + '>' + this.valuelist[x];
				}
				htm += '</select>';
			 	break;
			 case 'list':
			 	htm += '<select multiple size=5 '+dis+'' + multi_script + ' style="font-size:9pt">'; 
				for (var x in this.valuelist)
				{
					htm += '<option value="' + this.valuelist[x] + '" ' + (value.indexOf(this.valuelist[x])>=0?' selected ':'') + '>' + this.valuelist[x];
				}
				htm += '</select>'; 
			 	break;
			 case 'currency':
			 	htm += '<input '+dis+' class=flat type="text" size="4" value="' + value + '"' + input_script + '>'; 
			 	break; 
			 case 'number':
			 	htm += '<input '+dis+' class=flat  type="text" size="4" value="' + value + '"' + input_script + '>'; 
			 	break; 
			 case 'text':
			 	htm += '<input '+dis+' class=flat  type="text" size="15" value="' + value + '"' + input_script + '>'; 
			 	break; 
			 case 'boolean':
			 	htm += '<input '+dis+' type=checkbox ' + checkbox_script + '>'; 
			 	break; 
		 }
		 htm += '</td></tr>';
		 return htm;
	 }
	 
	this.Serialize = function ()
	{
	    var object = { 'id' : this.id,
	                   'property_name' : this.property_name,
	                   'property_type' : this.property_type,
	                   'property_each' : this.property_each,
	                   'type_name' : this.type_name,
	                   'property_item' : this.valuelist.join (',')
	                    }
	    return SerializeGeneric ('property', object);
	}
	
	this.ValueTable = function ()
	{
        registerTemplate ("property_date_row", property_date_row);
        var object = {'content' : '', 
                    'footer' : '&nbsp;Add item:<input class="flat" id="addcp"><input type="button" value="Add" onclick="AddCustomPropertyValue(value)">', 
                    'template' : 'dropdown_menu'};
	    for (var x in this.valuelist)
		{
			object.content += Factory.write ({'template' : "property_date_row", 'value' : this.valuelist[x]});
		} 
	    return Factory.write (object);
	}
	
	this.save = function ()
	{  
        var RequestParams = {  
            'aftersend' : CustomPropertyElement.OnSave ,
            'message' : 'Receiving save response...',
            'method' : 'POST',
            'postdata' : this.Serialize ()
                  }; 
        window.request.create (RequestParams); 
	}
	
    this.remove=function (value)
    {
        var result = new Array ();
        for (var x in this.valuelist)
        {
            if (this.valuelist[x]!=value) result.push (this.valuelist[x]);
        }
        this.valuelist = result;
        this.display_text = this.valuelist.join (',');
	}
	
    this.add=function (x)
    {
        this.valuelist.push (x);
        this.display_text = this.valuelist.join (',');
    }
 }
  
CustomPropertyElement.empty = function (name)
{ 
    var object = { 'id' : 0,
                   'property_name' : name,
                   'property_type' : 2,
                   'property_each' : '',
                   'type_name' : 'text',
                   'display_text' : ''
                    } 
	return new CustomPropertyElement(object);
}


 function CustomProperty (n,v,e)
 {
	 this.name=n;
	 this.value=v;
	 this.each=e;
 }
 
 CustomPropertyElement.OnSave = function ()
 {   
    Controller.SelectedProperty = parse4ID (window.http.responseText); 
    LoadGlobalPropertyList (function () { Controller.Reload(); }) ; 
 }
 
 
CustomPropertyElement.targetmask = function (target, data)
{
    var cpe;
    var name = data['property_name'];    
    if (target [name])  
    {  
        target [name].add (data['display_text']);  
    }   
    else 
    {  
        cpe=new CustomPropertyElement(data);  
        cpe.add (data['display_text']);  
        target.push (cpe);  
        target [name] = cpe; 
    }
} 

var property_date_row = '';
property_date_row += '<tr>';
property_date_row += '<td class=datarow><input onclick=\'RemoveCustomPropertyValue("(value)")\' checked id=ckcp type="checkbox"></td>';
property_date_row += '<td class=datarow width="100%"><a class="small">(value)</a></td>';
property_date_row += '</tr>'; 